
#include "CondA.h"
#include "Common/TableDataEx/DuplicateCfg.h"


CondA::CondA() :m_pDuplicate(nullptr), m_pVecCond(nullptr), m_result(-1), m_finish(false), m_limitSec(0)
{
	m_mapCondMons.clear();
	m_mapDieMons.clear();
	m_mapTotalMons.clear();
}

CondA::~CondA()
{

}

bool CondA::init(IDuplicate *pDuplicate, const VEC_INT32 *pCond, const string &strSubCond)
{
	if (nullptr == pDuplicate || nullptr == pCond)
	{
		return false;
	}
	_UNUSE(strSubCond);
	//1，限制时间，怪物1的ID，怪物1的数量，怪物2的ID，怪物2的数量……
	int32_t isize = pCond->size(); //怪物ID序列最少有一个,怪物ID和数量必须配对
	if (isize < 4)
	{
		LogErrFmtPrint("[logic] CondA::init....isize < 4, dupid:%u, sceneid:%u , isize:%d ", pDuplicate->GetDuplicateID(),pDuplicate->GetSceneID(),isize);
		return false;
	}
	m_pVecCond = pCond;
	m_pDuplicate = pDuplicate;
	m_limitSec = m_pVecCond->at(1);
	for (int32_t i = 2; i < isize;) 
	{
		int32_t idx1 = i;
		int32_t idx2 = i + 1;
		if (idx2 < isize)//怪物ID和数量 必须配对,有怪物ID，后面一定要有个数量
		{
			uint32_t monsId = m_pVecCond->at(idx1);
			int32_t monsNum = m_pVecCond->at(idx2); //数量为0 表示消灭全部怪，大于0 表示消灭具体的数量
			monsNum = (monsNum < 0) ? 0 : monsNum;
			//条件中配置的怪
			m_mapCondMons[monsId] = monsNum;
			m_mapTotalMons[monsId] = monsNum;
			m_mapDieMons[monsId] = 0;
		}

		i += 2;
	}

	return true;
}

void CondA::uninit()
{
	m_pDuplicate = nullptr;
	m_pVecCond = nullptr;
	m_finish = false;
	m_result = -1;
	m_limitSec = 0;
	m_mapCondMons.clear();
	m_mapDieMons.clear();
	m_mapTotalMons.clear();
}

bool CondA::finishCond()
{
	return m_finish;
}

int32_t CondA::result()
{
	return m_result;
}

int32_t CondA::limitSec()
{
	return m_limitSec;
}

void CondA::getMonsNum(MAP_UINT32_INT32 &mapMonsDieNum, MAP_UINT32_INT32 &mapMonsTotalNum)
{
	mapMonsDieNum = m_mapDieMons;
	mapMonsTotalNum = m_mapTotalMons;
	MAP_UINT32_INT32::iterator iterTotal = m_mapTotalMons.begin();
	for (; iterTotal != m_mapTotalMons.end(); ++iterTotal)
	{
		if (iterTotal->second <= 0) //为0 的时候代表所有的怪，这里从副本取一次怪物所有数量，但是这里的配置的怪物ID不能是技能召唤出来的
		{
			iterTotal->second = m_pDuplicate->GetMonsterTotalNum(iterTotal->first);
		}
	}
}

void CondA::onMonDie(uint32_t monsId, uint32_t num, int32_t leftNum)
{
	_UNUSE(leftNum);
	MAP_UINT32_INT32::iterator iterCond = m_mapCondMons.find(monsId);
	if (iterCond == m_mapCondMons.end())
	{
		return;
	}
	if (0 == iterCond->second) //为0 表示消灭全部指定ID的怪物
	{
		if (m_pDuplicate->GetMonsterLeftNum(iterCond->first) <= 0)
		{
			//指定ID的怪剩余数量为0，已经被全部消灭,直接移除
			m_mapCondMons.erase(iterCond);
		}		
	}
	else
	{
		iterCond->second -= num;
		if (iterCond->second <= 0)
		{
			//消灭了指定怪物ID指定的数量,直接移除
			m_mapCondMons.erase(iterCond);
		}
		
	}
	//增加怪物死亡
	addDieMons(monsId, num);
	//
	notifyMonsNum();
}

void CondA::addDieMons(uint32_t monsId, int32_t num)
{
	MAP_UINT32_INT32::iterator iter = m_mapDieMons.find(monsId);
	if (iter != m_mapDieMons.end())
	{
		iter->second += num;
	}
	else
	{
		m_mapDieMons[monsId] = num;
	}
}

void CondA::notifyMonsNum()
{
	if (nullptr == m_pDuplicate)
	{
		return;
	}
	
	//通知副本中的玩家 怪物序列
	Duplicate_MonsStateRsp rsp;
	MAP_UINT32_INT32::iterator iter = m_mapDieMons.begin();
	for (; iter != m_mapDieMons.end(); ++iter)
	{
		if (iter->second > 0)
		{
			DupMonsStateProto *proto = rsp.add_mon_state();
			if (nullptr != proto)
			{
				proto->set_mons_id(iter->first);
				proto->set_die_num(iter->second);
				proto->set_state(1);//1代表死亡
			}
		}		
	}
	m_pDuplicate->BroadCastData(EMODULE_ID_DUPLICATE, LOGIC_TO_CLIENT_DUPLICATE_MONSTER_STATE, &rsp);
}

void CondA::condUpdate()
{
	if (m_finish)
	{
		return;
	}
	m_result = checkCond();
	if (RET_SUCCESS == m_result || RET_FAIL == m_result)
	{
		m_finish = true;
	}
}

int32_t CondA::checkCond()
{
	if (nullptr == m_pVecCond || nullptr == m_pDuplicate)
	{
		return -1;
	}
	/*
	1，指定时间内消灭指定怪物序列，才能通关；超时失败
	2，指定时间内消灭所有怪物，才能通关；超时失败
	3，消灭所有怪物，才能通关；塔被怪物消灭失败
	4，达到指定时间点，怪物传送的数量小于目标值；怪物传送的数量达到目标值，或有BOSS进行传送，失败

	1，限制时间，怪物1的ID，怪物2的ID，……
	2，限制时间
	3，塔的ID
	4，限制时间，传送区域ID，目标值，BOSS1的ID，BOSS2的ID，……
	*/


	int32_t ret = -1;
	uint64_t curTime = g_GetGlobalServerTime()->UnixSec();
	int32_t interSec = (int32_t)(curTime - m_pDuplicate->StartBrushTime());

	//指定怪物ID序列是否全部死亡
	if (m_mapCondMons.size() <= 0) //指定怪物ID的序列全部死亡，胜利
	{
		ret = RET_SUCCESS;
	}
	else if (interSec >= m_limitSec)
	{
		//超时，失败
		ret = RET_FAIL;
	}

	return ret;
}